package com.example.userservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.dto.*;
import com.example.userservice.entity.User;
import com.example.userservice.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    // 注册
    @PostMapping("/register")
    public ApiResponse<Long> register(@RequestBody UserRegisterDTO registerDTO) {
        return userService.register(registerDTO);
    }

    // 登录
    @PostMapping("/login")
    public ApiResponse<String> login(@RequestBody LoginDTO loginDTO) {
        return userService.login(loginDTO);
    }

    // 分页用户列表
    @GetMapping
    public ApiResponse<Page<User>> listUsers(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        return userService.listUsers(page, size);
    }

    // 获取单个用户信息
    @GetMapping("/{userId}")
    public ApiResponse<User> getUserById(@PathVariable Long userId) {
        return userService.getUserById(userId);
    }

    // 更新用户信息
    @PutMapping("/{userId}")
    public ApiResponse<Void> updateUser(
            @PathVariable Long userId,
            @RequestBody UserUpdateDTO updateDTO) {
        updateDTO.setUserId(userId);
        return userService.updateUser(updateDTO);
    }

    // 密码重置
    @PostMapping("/reset-password")
    public ApiResponse<Void> resetPassword(@RequestBody PasswordResetDTO resetDTO) {
        return userService.resetPassword(resetDTO);
    }
}